<!--
 * @Author: zhoulei0232
 * @Date: 2022-12-08 00:37:02
 * @LastEditTime: 2023-03-23 22:57:20
 * @LastEditors: zhoulei0232
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:painter:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:painter:edit']"
          >修改</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="painterList">
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column
        label="姓名"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
        width="300"
      />
      <el-table-column
        label="画师QQ号"
        align="center"
        prop="userName"
        width="200"
      />
      <el-table-column label="状态" align="center" prop="is_pause">
        <template #default="scope">
          <dict-tag :options="stutasList" :value="scope.row.is_pause" />
        </template>
      </el-table-column>
      <el-table-column label="状态控制" align="center" prop="is_pause">
        <template #default="scope">
          <el-switch
            v-model="scope.row.is_pause"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:painter:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="画师姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入画师姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="画师账号" prop="userId">
              <el-select
                :style="{ width: '100%' }"
                v-model="form.userId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入用户名称搜索"
                :remote-method="remoteMethodUser"
                :loading="loadingUser"
              >
                <el-option
                  v-for="item in optionsUser"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="debouncedFn">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import {
  listPainter,
  updatePainter,
  getPainter,
  addPainter,
} from "@/api/business/painter";
import { listUser } from "@/api/business/user";

import { useDebounceFn } from "@vueuse/core";

const { proxy } = getCurrentInstance();

const painterList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const optionsUser = ref([]); //  用户搜索
const loadingUser = ref(false);

const data = reactive({
  stutasList: [
    {
      label: "冻结",
      value: "0",
      elTagClass: "",
      elTagType: "danger",
    },
    {
      label: "正常",
      value: "1",
      elTagClass: "",
      elTagType: "success",
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    userId: [
      { required: true, message: "画师账号不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules, stutasList } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listPainter(queryParams.value).then((response) => {
    painterList.value = response.data;

    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id;
  getPainter(id).then((response) => {
    form.value = {
      id: response.data.id,
      is_pause: response.data.is_pause,
      name: response.data.name,
      remarks: response.data.remarks,
      userId: response.data.user_id,
    };
    optionsUser.value = [
      {
        value: response.data.user_id,
        label: row.userName,
      },
    ];
    open.value = true;
    title.value = "修改";
  });
}

// 防抖操作
const debouncedFn = useDebounceFn(
  () => {
    submitForm();
  },
  1000,
  { maxWait: 3000 }
);

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePainter(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPainter(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 用户名远程搜索 */
function remoteMethodUser(query) {
  if (query) {
    loadingUser.value = true;
    let queryParams = {
      pageNum: 1,
      pageSize: 50,
      queryUserName: query,
    };
    listUser(queryParams).then((response) => {
      optionsUser.value = response.data.rows.map((item) => {
        return {
          label: item.username,
          value: item.id,
        };
      });

      loadingUser.value = false;
    });
  } else {
    optionsUser.value = [];
  }
}
/** 用户状态修改  */
function handleStatusChange(row) {
  let text = row.is_pause == "1" ? "启用" : "停用";

  proxy.$modal
    .confirm('确认要"' + text + '""' + row.name + '"画师吗?')
    .then(function () {
      let sendData = {
        id: row.id,
        is_pause: row.is_pause,
      };
      return updatePainter(sendData);
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.is_pause = row.is_pause == "1" ? "0" : "1";
    });
}

getList();
</script>
